<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;

class ZlFeedbackQuestionTags extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_feedback_question_tags';

    /**
     * 问题标签绑定
     * @param int $fqId
     * @param string $question
     * @throws \Exception
     */
    public function feedbackQuestionTagAdd(int $fqId, string $question): void
    {
        $tags = (new ZlFeedbackTag())->getTags(2, ['id', 'keyword']);

        $data = [];
        foreach ($tags as $tagItem)
        {
            $keywords = explode('，', $tagItem['keyword']);

            foreach ($keywords as $keyword)
            {
                $keyword = trim($keyword);
                if ($keyword && strrpos($question, $keyword) !== false)
                {
                    $data[] = [
                        'fq_id' => $fqId,
                        'tid' => (int)$tagItem['id']
                    ];
                    break;
                }
            }
        }

        if ($data)
        {   // 写入问题关联标签
            $this->saveAll($data);
        }
    }
}
